<?php
require_once APPLICATION_PATH . "/models/ModelBase.php";

class Users extends TT_Table_Abstract
{
    protected $_name = 'users';
	protected $_primary = 'uid';
	
	//Ham truy cap vao csdl, kiem tra username va password co ton tai trong csdl khong
	//tra ve object user neu cap username-password do ton tai
	//nguoc lai, tra ve null
	public function checkUser($username, $password)
	{
		$db = $this->getSessionDB();
		$sql = 'SELECT * FROM users WHERE name=? AND pass=?';
		return $db->fetchRow($sql, array($username,$password));		
	}
  
    
    public function checkInfo($uid)
    {
       $db = $this->getSessionDB();
        $sql = 'SELECT * FROM users_info WHERE uid=? ';
        return $db->fetchRow($sql, array($uid));         
    }
    
    public function setpass($uid,$pass)
    {
       $db = $this->getSessionDB(); 
        $sql="UPDATE users SET pass=? WHERE uid=?" ;
      return $db->query($sql,array($pass,$uid));
    }
    
    public function userinfo($uid,$info) //cap nhat loi tu gioi thieu
        {   
            $a=time();
            $db = $this->getSessionDB();
            $sql="UPDATE users_info SET user_info=? , updated=$a WHERE uid=?";
            return $db->query($sql,array($info,$uid));
        }
        
    public function manage($uid) //kiem tra user la admin moderator...
        {   
            $db = $this->getSessionDB();
            $sql="select manage FROM manage WHERE uid=?";
            return $db->fetchRow($sql,array($uid));
        }
        
    public function checkLoan($loan_id)      
    {
        $db = $this->getSessionDB();
        $sql = 'SELECT * FROM loans WHERE loan_id=?';
        return $db->fetchRow($sql, array($loan_id));
    }
    
    public function showall2($page,$page_rows)   // Phan trang
    {
        $page = (int)$page;
        $page_rows = (int)$page_rows;
        $db = $this->getSessionDB();
        $sql = "SELECT * FROM loans WHERE status = '0' ORDER BY loan_id DESC LIMIT $page,$page_rows ";
        return $db->fetchAll($sql) ;          
    }
    
    public function showall2_num()     // Dem so dong
    {
        $db = $this->getSessionDB();
        $sql = "SELECT count(loan_id) as num FROM loans WHERE status = '0' ORDER BY loan_id DESC ";
        return $db->fetchAll($sql) ;          
    }
}
